/*
 * Created by SharpDevelop.
 * User: Songwut Khamsuk
 * Date: 15/6/2552
 * Time: 13:14
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;

using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Internal.Templates;
using ICSharpCode.SharpDevelop.Project;
using Microsoft.Build.BuildEngine;
using DreamShieldBindingCompiler;

namespace DreamShieldBinding
{
	/// <summary>
	/// DreamShield project class. Handlings project loading and saving.
	/// </summary>
	public class DreamShieldProject : CompilableProject
	{
		public const string DefaultTargetsFile = @"$(DreamShieldToolPath)\DreamShield.Microsoft.Build.targets";
		delegate bool IsFileNameMatch(string fileName);

        /// <summary>
        /// This is the compiler
        /// </summary>
        public SetupCompilerEndPoint CompilerEndPoint { get; private set; }
		
		public DreamShieldProject(IMSBuildEngineProvider provider, string fileName, string projectName)
			: base(provider)
		{
            Initialize();

			this.Name = projectName;
			LoadProject(fileName);
		}
		
		public DreamShieldProject(ProjectCreateInformation info)
			: base(info.Solution)
        {
            Initialize();

			Create(info);
        }

        private void Initialize()
        {
#warning This have to be customized ... i'll add a key to the registry to hold that value
            CompilerEndPoint = new SetupCompilerEndPoint(@"C:\LKSoft\Applications\MS.Net\DreamShield\DSBin");
        }
		
		public override string Language {
			get { return DreamShieldLanguageBinding.LanguageName; }
		}
		
		public override LanguageProperties LanguageProperties {
			get { return LanguageProperties.None; }
		}
		
		public override void Start(bool withDebugging)
		{
			base.Start(false); // debugging not supported
		}
				
		public override System.Diagnostics.ProcessStartInfo CreateStartInfo()
		{
			switch (StartAction) {
				case StartAction.Project:
					return CreateStartInfo(InstallerFullPath);
				default:
					return base.CreateStartInfo();
			}
		}
		
		/// <summary>
		/// Adds the ability to creates DreamShield Object project items.
		/// </summary>
		public override ProjectItem CreateProjectItem(BuildItem item)
		{
			return base.CreateProjectItem(item);
		}
		
		public override ItemType GetDefaultItemType(string fileName)
		{
			if (string.Equals(Path.GetExtension(fileName), ".dscx", StringComparison.OrdinalIgnoreCase))
				return ItemType.Compile;
			else
				return base.GetDefaultItemType(fileName);
		}
		
		/// <summary>
		/// Gets the full path to the installer file that will be generated by
		/// the DreamShield compiler.
		/// </summary>
		public string InstallerFullPath {
			get {
				string outputPath = GetEvaluatedProperty("CompilerOutputDir") ?? String.Empty;
				string outputName = GetEvaluatedProperty("SFXSetupName") ?? String.Empty;
				string fileName = String.Concat(outputName, ".exe");
				return Path.Combine(Path.Combine(Directory, outputPath), fileName);
			}
		}
		
		/// <summary>
		/// Creates a DreamShieldProject with the default settings in its MSBuild file.
		/// </summary>
		protected override void Create(ProjectCreateInformation information)
		{
			base.Create(information);
			SetProperty("OutputType", "Package");
			string dreamShieldToolPath = @"$(SharpDevelopBinPath)\Tools\DreamShield";
			AddGuardedProperty("DreamShieldToolPath", dreamShieldToolPath, false);
			AddGuardedProperty("DreamShieldTargetsPath", @"$(DreamShieldToolPath)\DreamShield.Microsoft.Build.targets", false);
			
			//this.AddImport(DefaultTargetsFile, null);
		}
		
		/// <summary>
		/// AssemblyName must be implemented correctly - used when renaming projects.
		/// </summary>
		public override string AssemblyName {
			get { return GetEvaluatedProperty("SFXSetupName") ?? Name; }
			set { SetProperty("SFXSetupName", value); }
		}
		
		/// <summary>
		/// Returns a collection of FileProjectItems that match using the
		/// IsFileNameMatch delegate.
		/// </summary>
		ReadOnlyCollection<FileProjectItem> GetMatchingFiles(IsFileNameMatch match)
		{
			List<FileProjectItem> items = new List<FileProjectItem>();
			foreach (ProjectItem projectItem in Items) {
				FileProjectItem fileProjectItem = projectItem as FileProjectItem;
				if (fileProjectItem != null) {
					if (match(fileProjectItem.FileName)) {
						items.Add(fileProjectItem);
					}
				}
			}
			return new ReadOnlyCollection<FileProjectItem>(items);
		}
				
		/// <summary>
		/// Gets the MSBuild Property value for the given name.
		/// </summary>
		public string GetValue(string name)
		{
			string propertyValue;
			if (MSBuildEngine.MSBuildProperties.TryGetValue(name, out propertyValue)) {
				return propertyValue;
			}
			return null;
		}
	}
}
